package com.kupurui.asteacher.ui.mine.person;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.frame.ui.BasePhotoActivity;
import com.android.frame.util.AppJsonUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jph.takephoto.model.TResult;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 个人资料
 * Created by Administrator on 2017/7/17.
 */

public class PersonalInformationAty extends BasePhotoActivity {

    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.imgv_head)
    SimpleDraweeView imgvHead;
    @Bind(R.id.relatively_head)
    RelativeLayout relativelyHead;
    @Bind(R.id.tv_nickname)
    TextView tvNickname;
    @Bind(R.id.relatively_nickname)
    RelativeLayout relativelyNickname;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.relatively_phone)
    RelativeLayout relativelyPhone;

    private File img;

    @Override
    public int getLayoutId() {
        return R.layout.personl_information_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar, "个人资料");
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().center(UserManger.getId(),this,0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Teacher().center(UserManger.getId(),this,0);
    }

    @OnClick({R.id.relatively_head,R.id.relatively_nickname,R.id.relatively_phone})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.relatively_head://头像
                initPhotoDialog();
                break;
            case R.id.relatively_nickname://姓名
                startActivity(NameAty.class,null);
                break;
            case R.id.relatively_phone://电话
                startActivity(PhoneValidationAty.class,null);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                imgvHead.setImageURI(AppJsonUtil.getString(result,"img"));
                tvNickname.setText(AppJsonUtil.getString(result,"name"));
                tvPhone.setText(AppJsonUtil.getString(result,"phone"));
                break;
            case 1:
                showToast(AppJsonUtil.getResultInfo(result).getMessage());
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public void takeSuccess(TResult result) {
        imgvHead.setImageURI(Uri.fromFile(new File(result.getImage().getCompressPath())));
        img = new File(result.getImage().getCompressPath());
        showLoadingDialog("");
        new Teacher().changeImg(UserManger.getId(),img,this,1);
    }

    @Override
    public void takeFail(TResult result, String msg) {

    }

    @Override
    public void takeCancel() {

    }
}
